# -*- coding: utf-8 -*-
# @Time    : 2019/11/8 15:28
# @Author  : Jinwei
# @File    : reg_testlogic.py

from tools.log import LOG, error_cat
from tools.parseyaml import open_yaml
from tools.py_app import driver_fengzhuang as app_dev
import os, time


"""
解析测试步骤，按照需求执行测试用例，默认最后一个定位信息为断言
"""

class Caseparser():
    def __init__(self, driver, path):
        self.driver = driver
        self.path = path

    def open_yamlfile(self):
        return open_yaml(path=self.path)

    def exce_case(self):
        data = self.open_yamlfile()['data']
        time.sleep(5)
        case = app_dev(driver=self.driver)
        for i in range(len(data) - 1):
            lujing = data[i]['xpath']
            fangfa = data[i]['operate_type']
            key = data[i]['key']
            f = case.find_ele(fang=fangfa, lujing=lujing)
            if fangfa == 'click':
                f.click()
            elif fangfa == 'send_key':
                f.clear()
                f.send_keys(key)
            elif fangfa == 'text':
                f.text
            else:
                LOG.info("请检查你的测试步骤,可能含有不合法的操作方法")
            time.sleep(2)

        time.sleep(8)
        f = case.find_ele(fang=data[-1]['operate_type'], lujing=data[-1]['xpath'])
        if data[-1]['operate_type'] == 'text':
            duanyan = {'code': 0, 'data': f.text}
        else:
            duanyan = {'code': 1, 'data': "请检查最后一步是否为断言"}
            LOG.info("请检查你的测试步骤最后一步是否为断言")
        return duanyan


